package com.eat.fish_service.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eat.common_utils.IpAndPortUtil;
import com.eat.common_utils.R;
import com.eat.fish_service.entity.Dao.Disease;
import com.eat.fish_service.entity.Dao.Expert;
import com.eat.fish_service.entity.Dao.User;
import com.eat.fish_service.entity.VO.AnswerVo;
import com.eat.fish_service.entity.VO.DiseaseVo;
import com.eat.fish_service.mapper.DiseaseMapper;
import com.eat.fish_service.service.DiseaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eat.fish_service.service.ExpertService;
import com.eat.fish_service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LWB
 * @since 2022-05-04
 */
@Service
 public class DiseaseServiceImpl extends ServiceImpl<DiseaseMapper, Disease> implements DiseaseService {

    @Value("${uploadDisease}")
    String uploadDiseasePath;

    @Autowired
    private DiseaseService diseaseService;

    @Autowired
    private ExpertService expertService;

    @Autowired
    private UserService userService;

    @Override
    public R addDiseasePic(MultipartFile pic) {

        if (pic != null) {
            String fileName = System.currentTimeMillis() + pic.getOriginalFilename();

            //第一次运行的时候，这个文件所在的目录往往是不存在的，这里需要创建一下目录
            File dir = new File(uploadDiseasePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            //把浏览器上传的文件复制到希望的位置
            File targetFile = new File(uploadDiseasePath, fileName);
            try {
                pic.transferTo(targetFile);
            } catch (IOException e) {
                e.printStackTrace();
                return R.error().message("上传失败");
            }

            //服务器的ip和端口号
            String ipAndPort = IpAndPortUtil.getIpAndPort();
            String picUrl = "http://"+ipAndPort+"/getDiseasePic/"+fileName;

            //返回获取图片的url
            return R.ok().data("picUrl", picUrl);

        } else {
            return R.error().message("上传的文件为空！");
        }
    }

    @Override
    public R addDisease(Disease disease) {
        int insert = baseMapper.insert(disease);
        return insert==1?R.ok():R.error().message("添加失败");
    }

    @Override
    public R getUser(String userId,Integer status) {

        //构建条件
        QueryWrapper<Disease> wrapper = new QueryWrapper<>();

        wrapper.eq("user_id", userId);
        wrapper.eq("status", status);

        List<Disease> list = diseaseService.list(wrapper);

        //查询每个病单的专家，并封装为DiseaseVo类返回
        List<DiseaseVo> diseaseVoList = new ArrayList<>();
        for (Disease disease:list) {
            Expert expert = expertService.getById(disease.getExpertId());
            DiseaseVo diseaseVo = new DiseaseVo();
            diseaseVo.setExpertName(expert.getRealName());
            diseaseVo.setDisease(disease);
            diseaseVoList.add(diseaseVo);
        }

        return R.ok().data("diseaseVoList",diseaseVoList );
    }


    @Override
    public R getUserAll(String userId) {
        //构建条件
        QueryWrapper<Disease> wrapper = new QueryWrapper<>();

        wrapper.eq("user_id", userId);

        List<Disease> list = diseaseService.list(wrapper);

        //查询每个病单的专家，并封装为DiseaseVo类返回
        List<DiseaseVo> diseaseVoList = new ArrayList<>();
        for (Disease disease:list) {
            Expert expert = expertService.getById(disease.getExpertId());
            DiseaseVo diseaseVo = new DiseaseVo();
            diseaseVo.setExpertName(expert.getRealName());
            diseaseVo.setDisease(disease);
            diseaseVoList.add(diseaseVo);
        }

        return R.ok().data("diseaseVoList",diseaseVoList );
    }

    @Override
    public R getExpert(String expertId,Integer status) {

        //构建条件
        QueryWrapper<Disease> wrapper = new QueryWrapper<>();

        wrapper.eq("expert_id", expertId);
        wrapper.eq("status", status);

        List<Disease> list = diseaseService.list(wrapper);

        //查询每个病单的专家，并封装为DiseaseVo类返回
        List<DiseaseVo> diseaseVoList = new ArrayList<>();
        for (Disease disease:list) {
            User user = userService.getById(disease.getUserId());
            DiseaseVo diseaseVo = new DiseaseVo();
            diseaseVo.setUserName(user.getUsername());
            diseaseVo.setDisease(disease);
            diseaseVoList.add(diseaseVo);
        }

        return R.ok().data("diseaseVoList",diseaseVoList );
    }

    @Override
    public R answer(String answer,String id) {
        System.out.println(answer);
        AnswerVo answerVo =(AnswerVo) JSON.parseObject(answer,AnswerVo.class);

        Disease disease = new Disease();
        disease.setId(id);
        disease.setStatus(1);
        disease.setAnswer(answerVo.getAnswer());
        int i = baseMapper.updateById(disease);
        return i==1?R.ok():R.error().message("插入失败");
    }

    @Override
    public R getDiseaseVo(String diseaseId) {

        Disease disease = diseaseService.getById(diseaseId);

        User user = userService.getById(disease.getUserId());
        String expertId = disease.getExpertId();
        Expert expert = expertService.getById(expertId);

        DiseaseVo diseaseVo = new DiseaseVo();
        diseaseVo.setUserName(user.getUsername());
        diseaseVo.setDisease(disease);
        diseaseVo.setExpertName(expert.getRealName());

        return R.ok().data("diseaseVo", diseaseVo);
    }

    @Override
    public R getDiseaseAmount(String expertId,Integer status) {

        QueryWrapper<Disease> wrapper = new QueryWrapper<>();

        wrapper.eq("expert_id", expertId);
        wrapper.eq("status",status);

        int count = diseaseService.count(wrapper);

        return R.ok().data("count", count);
    }

}
